package com.tucanstubs.android_wheresmystuff.login;

import java.text.ParseException;
import com.tucanstubs.android_wheresmystuff.database.UserHelper;
import android.content.Context;

/**
 * The Login class is a gateway in between the users and the WheresMyStuff app.
 * The Login class has the ability to add a new user to the database, and the ability to 
 * authenticate an existing user.
 * @author team 37
 *@version 1.0 2/21/2013
 */
public abstract class Login {
	
	/**
	 * Determines based on the inputed user name and password whether the user should
	 * be granted access. Updates the user's lockCount if their password does not match
	 * @param context The current application context
	 * @param userName inputed user name
	 * @param password inputed password
	 * @return boolean indicating whether password is correct
	 * @throws NullPointerException thrown when either password, name, or both are null
	 * @throws LoginException thrown when the user account is locked or userName is not found
	 * @throws ParseException 
	 */
	
	public static boolean authenticateUser(Context context,String userName, String password) throws NullPointerException, LoginException, ParseException{
		if(password==null || userName==null  || password.length() == 0 || userName.length() == 0) {
			
			throw new NullPointerException("Please input a valid username and/or password.");
		}
		User u = UserHelper.get(context, userName);
		if(u==null) {
			
			throw new LoginException("You entered an incorrect username or password!");
		}
		if(u.isLocked()) {
			
			throw new LoginException("Your account is locked");
		}
		boolean result=password.equals(u.getPassword());
		if(!result) {
			
			u.updateLockCount();
			UserHelper.add(context, u);
			throw new LoginException("You entered an incorect username or password!");
		}
		return  result;
	}
	/**
	 * Creates a new user with the inputed information. Confirms that the users two inputed
	 * passwords match. If they match user is added and true is returned, otherwise no user
	 * is added and false is returned. Also checks to see that User name does not already exist,
	 * and that all inputed parameters are valid.
	 * @param context The current application context
	 * @param userName user's user name
	 * @param firstName user's first name
	 * @param lastName user's last name
	 * @param password user's password
	 * @param email user's email
	 * @param passwordCheck string to double check password for typing errors
	 * @return boolean indicating whether the passwords matched and the addition was successful
	 * @throws NullPointerException thrown when any inputed parameter is null
	 * @throws LoginException thrown when the user name is already taken 
	 * @throws ParseException 
	 */
	public static boolean add(Context context,String userName, String firstName, String lastName, String password, String email, String passwordCheck) throws NullPointerException, LoginException, ParseException{
		boolean isAdded = false;
		
		if(password==null || userName==null || email==null || passwordCheck==null || firstName==null || lastName==null) {
		
			throw new NullPointerException("Login error");
		}
		if(userName.length() > 12) {
			
			throw new LoginException("User Name is too long.");
		}
		if(password.length() < 6) {
			
			throw new LoginException("Password length is too short.");
		}
		if (password.equals("") || userName.equals("") || email.equals("") || passwordCheck.equals("") 
				|| firstName.equals("") || lastName.equals("")) {
			
			throw new LoginException("Empty field for required information.");
		}
		if(UserHelper.contains(context,userName)) {
			
			throw new LoginException("User name is already taken");
		}
		if(password.equals(passwordCheck)){
			UserHelper.add(context,new User(userName, firstName, lastName, password, email));
			isAdded = true;
		} else {
			
			throw new LoginException("Passwords do not match");
		}
		return isAdded;
	}
	
	/**
	 * Sets up the database
	 * @param context not used
	 * @throws ParseException parsing exception thrown by the database operator class
	 */
	public static void addDB(Context context) throws ParseException {
		if(!UserHelper.contains(context,"testuser"))
			UserHelper.add(context,new User("testuser","Bill","Clinton","123456","2swag4u@thuglife.edu"));
		if(!UserHelper.contains(context,"admin"))
			UserHelper.add(context, new Admin("admin", "Shrek", "Donkey", "admin1", "thisismahswamp@tupac.edu"));
	}
}
